//
//  NetworkTools.swift
//  AFNetworking封装
//
//  Created by iwan on 2017/4/17.
//  Copyright © 2017年 iwan. All rights reserved.
//

//import UIKit
import AFNetworking

// 定义枚举
enum RequestType : String{
    case GET = "GET"
    case POST = "POST"
    
}


class NetworkTools: AFHTTPSessionManager {
    
    //MARK:- 声明一个单利模式
    static let shareInstance:NetworkTools = {
        
        let tools = NetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        return tools
        
    }()
    
}

//MARK:- 请求方式处理
extension NetworkTools{
    
    func request(requestType:RequestType,urlString:String,parameters:[String:Any],finished:@escaping (_ result:Any?,_ error:Any?) -> ()){
        
        
        //1 成功回调
        let successCallback = { (task:URLSessionDataTask, result:Any) in 
            finished(result,nil)}
        //2 失败回调
        let failureCallback = { (task:URLSessionDataTask?, error:Error) in
            finished(nil,error)
        }
        
        // 请求类型
        if requestType == .GET {
            get(urlString, parameters: parameters, progress: nil, success:successCallback,failure:failureCallback)
            
        }else{
            
            post(urlString, parameters: parameters, progress: nil, success:successCallback,failure:failureCallback)
        }
        
        
        
        
        
    }
    
}
